package sis.ra.yahoor.result;

import java.util.ArrayList;
import java.util.HashMap;

import sis.ra.partofspeech.wordindex;
import sis.ra.utility.Utils;


public class extractPatternVerb {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String filepath="C:\\Documents and Settings\\I820753\\Desktop\\dataset\\result1006.snippetCPonly";
		String content=Utils.readFile(filepath);
		String [] lines=content.split("\n");
		String filename="";
		int count=0;
		int countbe=0;
		int counthave=0;
		int countv=0;
		int countnonverb=0;
		for (int i=0;i<lines.length;i++)
		{
//			System.out.println(lines[i]);
			String line=lines[i];
			if (line.contains(".txt"))
			{
				System.out.println("files: "+line);
//				Utils.writeToFile("C:\\Documents and Settings\\I820753\\Desktop\\dataset\\result1006.verbbe", line,true,true);
//				Utils.writeToFile("C:\\Documents and Settings\\I820753\\Desktop\\dataset\\result1006.verbhave", line,true,true);
//				Utils.writeToFile("C:\\Documents and Settings\\I820753\\Desktop\\dataset\\result1006.verbv", line,true,true);
//				Utils.writeToFile("C:\\Documents and Settings\\I820753\\Desktop\\dataset\\result1006.nonverb", line, true, true);
//				Utils.writeToFile("C:\\Documents and Settings\\I820753\\Desktop\\dataset\\result1006.verb", line, true, true);
				filename=line;
			}else
			{
				if (line.length()<2) continue;
				int productpos=line.indexOf("PRODUCT");
				int companypos=line.indexOf("COMPANY");
				int start=productpos;
				int end=companypos;
				int offset=0;
				int productoffset=0;
				int distance=0;
				String pattern="";
				if (productpos>companypos)
				{
					offset=line.substring(companypos, productpos).split(" ").length;
					pattern=line.substring(companypos, productpos+7);
					start=companypos;
					end=productpos;
					distance=productpos-companypos;
				}else
				{
					offset=line.substring(productpos, companypos).split(" ").length;
					pattern=line.substring(productpos, companypos+7);
					distance=companypos-productpos;
				}
				
				HashMap<Integer,wordindex> posresult=parseYahooResults.pos(line, "text");
				ArrayList<String> resultlist=parseYahooResults.getResultList (posresult);
				String result="";
				String analysis="";
				boolean containVerb=false;
				String verb="";
				int verbpos=0;
				
				//verb
				for (int n=0;n<resultlist.size();n++)
				{
					analysis=analysis+resultlist.get(n);
					int current=Integer.parseInt(resultlist.get(n).split("\t")[0]);
					if (current>=start&&current<=end)
					{
						result=result+resultlist.get(n);
						if (resultlist.get(n).contains("V-")&&verbpos==0)//||resultlist.get(n).contains("V-Pres-Sg-be"))
						{
							verb=resultlist.get(n).split("\t")[1];
							verbpos=line.indexOf(verb);
							if (productpos>verbpos)
								productoffset=line.substring(verbpos, productpos).split(" ").length;
							else
								productoffset=line.substring(productpos, verbpos).split(" ").length;
							containVerb=true;
						}else if (resultlist.get(n).contains("V-"))
						{
							if (verb.equals("are")||verb.equals("is")||verb.equals("have")||verb.equals("has")||verb.equals("being")
									||verb.equals("been")||verb.equals("were")||verb.equals("was")||verb.equals("had"))
							{
								verb=resultlist.get(n).split("\t")[1];
								verbpos=line.indexOf(verb);
							}
						}
					}
				}
				
				String stemed=extractPattern.stem(verb);
//				//verb have
//				for (int n=1;n<resultlist.size();n++)
//				{
//					result=result+resultlist.get(n);
//					if (resultlist.get(n-1).contains("V-Pres-3-Sg")&&resultlist.get(n).contains("V-Past"))//"V-Pres-Sg-be"))
//					{
//						containVerb=true;
//						verb=resultlist.get(n).split("\t")[1];
//						verbpos=line.indexOf(verb);
//						break;
//					}
//				}
				
//				//verb is
//				for (int n=0;n<resultlist.size();n++)
//				{
//					result=result+resultlist.get(n);
//					if (resultlist.get(n).contains("V-Pres-Sg-be"))
//					{
//						containVerb=true;
//						verb=resultlist.get(n).split("\t")[1];
//						verbpos=line.indexOf(verb);
//						break;
//					}
//				}
				
				//verb is + V-PrPart
//				for (int n=1;n<resultlist.size();n++)
//				{
//					result=result+resultlist.get(n);
//					if (result.contains("V-Pres-Sg-be")&&resultlist.get(n).contains("V-PrPart"))
//					{
//						containVerb=true;
//						verb=resultlist.get(n).split("\t")[1];
//						verbpos=line.indexOf(verb);
//						break;
//					}
//				}
//				System.out.println(productpos+" "+companypos);
//				System.out.println(line);
//				System.out.println(result);
				
				if (containVerb&&((productpos>verbpos&&verbpos>companypos)||(productpos<verbpos&&verbpos<companypos)))
				{
					System.out.println(verbpos+"\t"+verb+"\t"+line);
					count=count+1;
//					System.out.println(analysis);
//					if (verb.equals("is")||verb.equals("are")||verb.equals("was")||verb.equals("were")||verb.equals("been")||verb.equals("being")||verb.equals("be"))
//						{
//							countbe=countbe+1;
//		//					Utils.writeToFile("C:\\Documents and Settings\\I820753\\Desktop\\dataset\\result1006.verbbe", verbpos+"\t"+verb+"\t"+line,true,true);
//						}
//					else if (verb.equals("have")||verb.equals("has")||verb.equals("had"))
//						{
//							counthave=counthave+1;
//		//					Utils.writeToFile("C:\\Documents and Settings\\I820753\\Desktop\\dataset\\result1006.verbhave", verbpos+"\t"+verb+"\t"+line,true,true);
//						}
//					else 
//						{
//							countv=countv+1;
//		//					Utils.writeToFile("C:\\Documents and Settings\\I820753\\Desktop\\dataset\\result1006.verbv", verbpos+"\t"+verb+"\t"+line,true,true);
//						}
					Utils.writeToFile("C:\\Documents and Settings\\I820753\\Desktop\\dataset\\result1006.verbv2", offset+"\t"+productoffset+"\t"+stemed+"\t"+verb+"\t"+line.substring(3),true,true);
					
				}else
				{
//					countnonverb=countnonverb+1;
//					Utils.writeToFile("C:\\Documents and Settings\\I820753\\Desktop\\dataset\\result1006.nonverb", line, true, true);
				}
			}
			
		}
		System.out.println(count+"="+countbe+"+"+counthave+"+"+countv+" "+countnonverb);
	}
}
